package com.nowadays.mobile.mvp.model;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Looper;
import android.util.Log;

import com.nowadays.mobile.MainApplication;
import com.nowadays.mobile.R;
import com.nowadays.mobile.db.DbProvider;
import com.nowadays.mobile.model.FocusDayList;
import com.nowadays.mobile.mvp.presenter.executor.MainThread;
import com.nowadays.mobile.utils.CommonUtils;
import com.nowadays.mobile.utils.ConstsDefine;
import com.nowadays.mobile.utils.FocusUtils;
import com.nowadays.mobile.utils.SettingInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainInteractorImpl implements MainInteractor {
  private MainThread mainThread;

  public MainInteractorImpl(MainThread mainThread) {
    this.mainThread = mainThread;
  }

  @Override
  public HashMap<Integer, Integer> getVoiceInfo() {
    return CommonUtils.mSoundMap;
  }

  @SuppressWarnings("deprecation")
  @Override
  public void initVoice() {
//    mainThread.post(new Runnable() {
//      @Override
//      public void run() {
        CommonUtils.mSoundMap = new HashMap<>(3);
        CommonUtils.mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
        CommonUtils.mSoundMap.put(ConstsDefine.KEY_TICK_SOUND, CommonUtils.mSoundPool.load(MainApplication.getContext(), R.raw.ticktock, 1));
        CommonUtils.mSoundMap.put(ConstsDefine.KEY_FINISH_ALARM_SOUND, CommonUtils.mSoundPool.load(MainApplication.getContext(), R.raw.alarm_finish, 1));
        CommonUtils.mSoundMap.put(ConstsDefine.KEY_REST_ALARM_SOUND, CommonUtils.mSoundPool.load(MainApplication.getContext(), R.raw.alarm_rest, 1));
//      }
//    });
  }

  @Override
  public Focus getFocus() {
    return Focus.getInstance();
  }

  @Override
  public void initFocus() {
    Log.d("TAG", "threadId:" + Thread.currentThread().getId() + ",isMainThread:" + (Looper.getMainLooper() == Looper.myLooper()));
    Focus focus = Focus.getInstance();
    SettingInfo info = SettingInfo.getInstance();
    int focusTime = info.getFocusTime();
    Log.d("TAG", "initFocus:focusTime=" + focusTime);
    int restTime = info.getRestTime();
    boolean isPlayTick = info.getIsPlayTickSound();
    boolean isPlayAlarm = info.getIsPlayAlarmSound();
    boolean isPreventScreen = info.getIsPreventLockScreen();
    boolean isVibrate = info.getIsVibrate();
    focus.setFocusTime(focusTime);
    focus.setRestTime(restTime);
    focus.setPlayAlaram(isPlayAlarm);
    focus.setPlayTick(isPlayTick);
    focus.setPreventScreen(isPreventScreen);
    focus.setVibrate(isVibrate);
    focus.setAutoFocus(info.getIsAutoFocus());
    focus.setLongRestDelay(info.getLongRestDelay());
    focus.setLongRestTime(info.getLongRestTime());
  }

  @Override
  public void saveFocus(int focusTime) {
    DbProvider.getInstance(MainApplication.getContext()).saveFocus(focusTime);
  }

  @Override
  public List<FocusDayList> queryFocusList() {
    ArrayList<com.nowadays.mobile.model.Focus> focusList = DbProvider.getInstance(MainApplication.getContext()).queryFocusList();
    List<FocusDayList> data = FocusUtils.listFocusByDay(focusList);
    return data;
  }
}
